package cn.iocoder.yudao.module.tms.service.statistics;

import cn.iocoder.yudao.module.tms.controller.admin.statistics.vo.*;
import cn.iocoder.yudao.module.tms.dal.mysql.statistics.StatisticsMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAdjusters;
import java.util.List;

/**
 * TMS统计分析 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Slf4j
public class StatisticsServiceImpl implements StatisticsService {

    @Resource
    private StatisticsMapper statisticsMapper;

    @Override
    public LocalDateTime[] getTimeRange(StatisticsQueryReqVO reqVO) {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime startTime;
        LocalDateTime endTime;

        switch (reqVO.getTimeRangeType()) {
            case "DAY":
                // 今日：00:00:00 到 23:59:59
                startTime = now.toLocalDate().atStartOfDay();
                endTime = now.toLocalDate().atTime(23, 59, 59);
                break;

            case "WEEK":
                // 本周：周一00:00:00 到周日23:59:59
                startTime = now.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY))
                        .toLocalDate().atStartOfDay();
                endTime = now.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY))
                        .toLocalDate().atTime(23, 59, 59);
                break;

            case "MONTH":
                // 本月：1日00:00:00 到最后一天23:59:59
                startTime = now.with(TemporalAdjusters.firstDayOfMonth())
                        .toLocalDate().atStartOfDay();
                endTime = now.with(TemporalAdjusters.lastDayOfMonth())
                        .toLocalDate().atTime(23, 59, 59);
                break;

            case "QUARTER":
                // 近3个月：3个月前的1日00:00:00 到今天23:59:59
                startTime = now.minusMonths(3).with(TemporalAdjusters.firstDayOfMonth())
                        .toLocalDate().atStartOfDay();
                endTime = now.toLocalDate().atTime(23, 59, 59);
                break;

            case "CUSTOM":
                // 自定义：用户指定的时间范围
                startTime = reqVO.getStartTime();
                endTime = reqVO.getEndTime();
                break;

            default:
                // 默认今日
                startTime = now.toLocalDate().atStartOfDay();
                endTime = now.toLocalDate().atTime(23, 59, 59);
                break;
        }

        return new LocalDateTime[]{startTime, endTime};
    }

    @Override
    public List<BandUsageStatisticsVO> getBandUsageStatistics(StatisticsQueryReqVO reqVO) {
        LocalDateTime[] timeRange = getTimeRange(reqVO);
        return statisticsMapper.selectBandUsageStatistics(timeRange[0], timeRange[1]);
    }

    @Override
    public List<BandInspectionStatisticsVO> getBandInspectionStatistics(StatisticsQueryReqVO reqVO) {
        LocalDateTime[] timeRange = getTimeRange(reqVO);
        return statisticsMapper.selectBandInspectionStatistics(timeRange[0], timeRange[1]);
    }

    @Override
    public List<OrderInspectionStatisticsVO> getOrderInspectionStatistics(StatisticsQueryReqVO reqVO) {
        LocalDateTime[] timeRange = getTimeRange(reqVO);
        return statisticsMapper.selectOrderInspectionStatistics(timeRange[0], timeRange[1]);
    }

    @Override
    public List<CustomerCooperationStatisticsVO> getCustomerCooperationStatistics(StatisticsQueryReqVO reqVO) {
        LocalDateTime[] timeRange = getTimeRange(reqVO);
        return statisticsMapper.selectCustomerCooperationStatistics(timeRange[0], timeRange[1]);
    }

    @Override
    public StatisticsOverviewVO getStatisticsOverview(StatisticsQueryReqVO reqVO) {
        LocalDateTime[] timeRange = getTimeRange(reqVO);
        return statisticsMapper.selectStatisticsOverview(timeRange[0], timeRange[1]);
    }
}
